/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.model.questionnaire;

import com.blandware.atleap.model.core.Page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Represents a questionnaire, which may be filled by user.</p>
 * <p><a href="Questionnaire.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.3 $ $Date: 2006/06/25 14:44:39 $
 * @struts.form include-all="false" extends="BaseForm"
 * resetMappedProperties="false"
 * @hibernate.joined-subclass table="`al_questionnaire_questionnaire`"
 * lazy="false"
 * @hibernate.joined-subclass-key column="`page_id`"
 */
public class Questionnaire extends Page {
	//~ Instance variables

	/**
	 * Annotation for this questionnaire
	 */
	protected Map annotation = new HashMap();

    /**
     * Whether this questionnaire is fixed and cannot be edited more
     */
    protected Boolean fixed = Boolean.FALSE;

    /**
     * List of questions in this questionnaire
     */
    protected List questions = new ArrayList();

    /**
     * List of filled questionnaires for this one
     */
    protected List filledQuestionnaires = new ArrayList();

	//~ Methods

    /**
     * Returns whether this questionnaire is fixed and cannot be edited more.
     *
     * @return if fixed
	 * @hibernate.property column="`fixed`" not-null="true" type="true_false"
     */
    public Boolean getFixed() {
        return fixed;
    }

    /**
     * Sets whether this questionnaire is fixed and cannot be edited more.
     *
     * @param fixed value to set
     */
    public void setFixed(Boolean fixed) {
        this.fixed = fixed;
    }

    /**
     * Returns a list of questions.
     *
     * @return list of questions
     * @hibernate.bag name="questions" inverse="true" lazy="true"
     * cascade="delete" outer-join="false"
     * order-by="`pos`"
	 * @hibernate.collection-key column="`questionnaire_id`"
	 * @hibernate.collection-one-to-many class="com.blandware.atleap.model.questionnaire.Question"
     */
    public List getQuestions() {
        return questions;
    }

    /**
     * Sets a list of questions.
     *
     * @param questions list to set
     */
    public void setQuestions(List questions) {
        this.questions = questions;
    }

    /**
     * Adds question to this quetionnaire.
     *
     * @param question the question to be added
     */
	public void addQuestion(Question question) {
		question.setQuestionnaire(this);
		questions.add(question);
	}

    /**
     * Updates changed question.
     *
     * @param question the question to be updated
     * @return question that has been replaced
     */
	public Question updateQuestion(Question question) {
        Question oldQuestion = null;
		if ( !question.getQuestionnaire().equals(this) ) {
            int index = question.getQuestionnaire().questions.indexOf(question);
            if (index != -1)
                oldQuestion = (Question) question.getQuestionnaire().questions.remove(index);
			questions.add(question);
			question.setQuestionnaire(this);
		}
        return oldQuestion;
	}

    /**
     * Removes question from this questionnaire.
     *
     * @param question the question to be removed
     */
	public void removeQuestion(Question question) {
		questions.remove(question);
	}

    /**
     * Returns a list of filled questionnaires.
     *
     * @return list of filled questionnaires
     * @hibernate.bag name="filledQuestionnaires" inverse="true" lazy="true"
     * cascade="delete" outer-join="false"
	 * @hibernate.collection-key column="`questionnaire_id`"
	 * @hibernate.collection-one-to-many class="com.blandware.atleap.model.questionnaire.FilledQuestionnaire"
     */
    public List getFilledQuestionnaires() {
        return filledQuestionnaires;
    }

    /**
     * Sets a list of filled questionnaires.
     *
     * @param filledQuestionnaires list to set
     */
    public void setFilledQuestionnaires(List filledQuestionnaires) {
        this.filledQuestionnaires = filledQuestionnaires;
    }

    /**
     * Adds filled questionnaire to this quetionnaire.
     *
     * @param filledQuestionnaire the filled questionnaire to be added
     */
	public void addFilledQuestionnaire(FilledQuestionnaire filledQuestionnaire) {
		filledQuestionnaire.setQuestionnaire(this);
		filledQuestionnaires.add(filledQuestionnaire);
	}

    /**
     * Removes filled questionnaire from this questionnaire.
     *
     * @param filledQuestionnaire the filled questionnaire to be removed
     */
	public void removeFilledQuestionnaire(FilledQuestionnaire filledQuestionnaire) {
		filledQuestionnaires.remove(filledQuestionnaire);
	}

	// Internal field values

	/**
     * Gets annotations map.
     *
	 * @return mapping from locale identifiers to annotations
	 * @struts.form-field
	 */
	public Map getAnnotation() {
		return annotation;
	}

    /**
     * Sets annotations map.
     *
     * @param annotation mapping from locale identifiers to annotations
     */
	public void setAnnotation(Map annotation) {
		this.annotation = annotation;
	}
}
